package org.blame.rabbitmqadvancedfeature.config;

import org.blame.rabbitmqadvancedfeature.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Configuration
public class RabbitConfig {
//    消息确认

//    创建队列
    @Bean("ackQueue")
    public Queue ackQueue(){
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }
//    创建交换机
    @Bean("ackExchange")
    public DirectExchange ackExchange(){
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).durable(true).build();
    }
//    队列和交换机进行绑定
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackQueue") Queue queue,@Qualifier("ackExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("ack");
    }

    //    创建队列
    @Bean("presQueue")
    public Queue presQueue(){
        return QueueBuilder.durable(Constants.PRES_QUEUE).build();
    }
    //    创建交换机
    @Bean("presExchange")
    public DirectExchange presExchange(){
        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).build();
    }
    //    队列和交换机进行绑定
    @Bean("presBinding")
    public Binding presBinding(@Qualifier("presQueue") Queue queue,@Qualifier("presExchange") Exchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("pres").noargs();
    }

    @Bean("confirmQueue")
    public Queue confirmQueue(){
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }

    @Bean("confirmExchange")
    public DirectExchange confirmExchange(){
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }

    @Bean("confirmBlinding")
    public Binding confirmBlinding(@Qualifier("confirmQueue")Queue queue,@Qualifier("confirmExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("123");
    }

    @Bean("retryQueue")
    public Queue retryQueue(){
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryExchange")
    public DirectExchange retryExchange(){
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).build();
    }
    @Bean("retryBlinding")
    public Binding retryBlinding(@Qualifier("retryQueue") Queue queue,
                                 @Qualifier("retryExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("123");
    }


    @Bean("ttlQueue2")
    public Queue ttlQueue2(){

        //设置过期队列的方法一
        int ttlTime=10000;//设置过期时间为20秒
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(ttlTime).build();

        //设置过期队列方法二
//        Map<String, Object> argument=new HashMap<>();
//        argument.put("x-message-ttl",20000);
//        return QueueBuilder.durable(Constants.TTL_QUEUE2).withArguments(argument).build();

    }
    @Bean("ttlQueue")
    public Queue ttlQueue(){
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }

    @Bean("ttlExchange")
    public DirectExchange ttlExchange(){
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }

    @Bean("ttlBlinding")
    public Binding ttlBlinding(@Qualifier("ttlQueue") Queue queue,
                               @Qualifier("ttlExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("123");
    }

//    @Bean("ttlBlinding2")
//    public Binding ttlBlinding2(@Qualifier("ttlQueue2") Queue queue,
//                               @Qualifier("ttlExchange") DirectExchange directExchange){
//        return BindingBuilder.bind(queue).to(directExchange).with("123");
//    }

    @Bean("transQueue")
    public Queue transQueue(){
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }
}
